package Games;

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.util.Timer;
import java.util.TimerTask;

public class SnakeStage extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    int x_width = 600, x_height = 400;
    Canvas canvas = new Canvas(x_width, x_height);
    Snake snk = new Snake(x_width / 10, x_height / 10);
    @Override
    public void start(Stage primaryStage) {
        //画布挂载
        AnchorPane pane = new AnchorPane(canvas);
        Scene scene = new Scene(pane);
        //窗口初始化
        primaryStage.setScene(scene);
        primaryStage.setTitle("贪吃蛇游戏 v1.0");
        primaryStage.show();

        /*-------------以下是游戏逻辑--------------*/

        //画布处理
        canvas.requestFocus();
        canvas.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
            switch (event.getCode().ordinal()){
                case 16: snk.SetDirection(Direction.Left); break;
                case 17: snk.SetDirection(Direction.Up); break;
                case 18: snk.SetDirection(Direction.Right); break;
                case 19: snk.SetDirection(Direction.Down); break;
                default: break;
            }
        });
        canvas.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
            if(event.getCode() == KeyCode.SPACE)
            {
                snk.Reset();
                this.start_main_loop();
            }
        });
        //启动游戏主循环
        this.start_main_loop();
    }


    /**
     * 启动游戏主循环
     */
    private void start_main_loop()
    {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                GraphicsContext g = canvas.getGraphicsContext2D();
                try
                {
                    snk.Move();

                    //绘制背景
                    g.setFill(Color.BLACK);
                    g.fillRect(0,0, x_width, x_height);
                    //绘制蛇身
                    g.setFill(Color.GREEN);
                    for(java.awt.Point p: snk.Nodes){
                        g.fillRect(p.y * 10, p.x * 10, 9, 9);
                    }
                    //绘制食物
                    g.setFill(Color.YELLOW);
                    g.fillRect(snk.Food.y * 10, snk.Food.x * 10, 9,9);
                }
                catch (Exception ex)
                {
                    timer.cancel();
                    timer.purge();

                    //打印提示
                    String msg = ex.getMessage() + "（按空格键重新开始）";
                    System.out.println(msg);
                    //计算字符串长宽
                    Font font = new Font("微软雅黑",20);
                    Text text = new Text(msg);
                    text.setFont(font);
                    Bounds bounds = text.getBoundsInLocal();
                    //绘制结束页面
                    int msg_width = (int)bounds.getWidth();
                    int msg_height = (int)bounds.getHeight();
                    int x = (x_width - msg_width) / 2;
                    int y = (x_height - msg_height) / 2;
                    g.setFont(font);
                    g.setFill(Color.WHITE);
                    g.fillText(msg, x, y);
                }
            }
        }, 0,100); //延迟0，间隔100ms
    }
}
